package org.tis.tools.senior.module.developer.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.senior.module.core.web.util.ShiroUtil;
import org.tis.tools.senior.module.developer.controller.request.DeliveredNewProfilesRequest;
import org.tis.tools.senior.module.developer.controller.request.DeliveryOutExeclRequest;
import org.tis.tools.senior.module.developer.controller.request.MergeDeliveryRequest;
import org.tis.tools.senior.module.developer.controller.request.SDeliveryUpdateRequest;
import org.tis.tools.senior.module.developer.entity.SDelivery;
import org.tis.tools.senior.module.developer.entity.SSvnAccount;
import org.tis.tools.senior.module.developer.entity.vo.DeliveryWorkitemDetail;
import org.tis.tools.senior.module.developer.entity.vo.MergeReportDetail;
import org.tis.tools.senior.module.developer.exception.DeveloperException;
import org.tis.tools.senior.module.developer.service.ISDeliveryService;
import org.tmatesoft.svn.core.SVNException;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.List;


/**
 * sDelivery的Controller类
 *
 * @author Auto Generate Tools
 * @date 2018/06/20
 */
@RestController
@RequestMapping("/deliveries")
@Validated
@Api(value = "投放申请controller", tags = {"投放申请操作接口"})
public class SDeliveryController extends BaseController<SDelivery> {

    @Autowired
    private ISDeliveryService sDeliveryService;

    @GetMapping("/{guid}")
    public ResultVO detail(@PathVariable @NotBlank(message = "投放申请的guid不能为空") String guid) {
        SDelivery sDelivery = sDeliveryService.selectById(guid);
        if (sDeliveryService == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", sDelivery);
    }

    /**
     * 查询登录用户的所有投放申请
     *
     * @param page
     * @return
     */
    @ApiOperation(value = "查询登录用户的所有投放申请", notes = "body传参")
    @PostMapping("/list")
    public ResultVO list(@RequestBody
                             @ApiParam(name = "page", value = "分页对象", required = true)
                             @Validated SmartPage<DeliveryWorkitemDetail> page) {

        SSvnAccount svnAccount = ShiroUtil.getUser();
        if (svnAccount == null) {
            throw new DeveloperException("尚未登录，请登录后再试！");
        }
        Page<DeliveryWorkitemDetail> deliveryDetailPage = new Page<DeliveryWorkitemDetail>
                (page.getPage().getCurrent(), page.getPage().getSize(),
                        page.getPage().getOrderByField(), page.getPage().getAsc());

        return ResultVO.success("查询成功", sDeliveryService.getDeliveryAll(deliveryDetailPage, getWrapper(page.getCondition()), svnAccount));
    }

    /**
     * 获取合并投放清单信息
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "获取合并投放清单信息", notes = "body传参")
    @PostMapping("/merge/info")
    public ResultVO mergeInfo(@RequestBody @Validated MergeDeliveryRequest request) {
        return ResultVO.success(sDeliveryService.getMergeInfo(request, ShiroUtil.getUser().getUserId()));
    }

    /**
     * 确认合并申请
     * @param id
     * @return
     */
    //@OperateLog(type = OperateType.UPDATE, desc = "确认合并申请")
    @ApiOperation(value = "确认合并申请", notes = "url传参")
    @PutMapping("/{id}/manualMerge")
    public ResultVO merge(@PathVariable
                              @ApiParam(name = "id", value = "投放申请guid", required = true)
                              @NotNull(message = "id不能为空") Integer id) {

        sDeliveryService.merge(id);
        return ResultVO.success("确认已合并！");
    }

    /**
     * 取消合并申请
     * @param id
     * @return
     */
    //@OperateLog(type = OperateType.UPDATE, desc = "取消合并申请")
    @ApiOperation(value = "取消合并申请", notes = "url传参")
    @PutMapping("/{id}/unmerge")
    public ResultVO unmerge(@PathVariable @NotBlank(message = "id不能为空") String id) {
        sDeliveryService.unmerge(id);
        return ResultVO.success("已取消合并！");
    }

    /**
     * 查询一条投放申请中的工程名
     *
     * @param guidDelivery
     * @return
     */
    @GetMapping("/{guidDelivery}/projectName")
    public ResultVO projectName(@PathVariable @NotBlank(message = "id不能为空") String guidDelivery) {

        return ResultVO.success("查询成功", sDeliveryService.selectDeliveryProName(guidDelivery));
    }

    /**
     * 查询工作项所要追加的投放申请集合
     *
     * @param workitemGuid
     * @return
     */
    @ApiOperation(value = "查询工作项所要追加的投放申请集合", notes = "url传参")
    @GetMapping("/{workitemGuid}/addTo")
    public ResultVO addToDelivery(@PathVariable
                                      @ApiParam(name = "workitemGuid", value = "工作项guid", required = true)
                                      @NotNull(message = "工作项id不能为空")
                                              Integer workitemGuid) throws ParseException {

        return ResultVO.success("查询成功！", sDeliveryService.selectAddToDelivery(workitemGuid));
    }

    /**
     * 删除投放申请及投放的代码
     *
     * @param guidDelivery
     * @return
     */
    //@OperateLog(type = OperateType.DELETE, desc = "删除投放申请")
    @ApiOperation(value = "删除投放申请", notes = "url传参")
    @DeleteMapping("/{guidDelivery}")
    public ResultVO deleteDeliveryAndDeliveryList(@PathVariable
                                                      @ApiParam(name = "guidDelivery", value = "投放申请guid", required = true)
                                                      @NotNull(message = "投放申请id不能为空")
                                                          Integer guidDelivery) throws SVNException {

        sDeliveryService.deleteDeliveryAndDeliveryList(guidDelivery);
        return ResultVO.success("删除成功");
    }

    /**
     * 根据投放申请guid查询所投放的代码集合
     *
     * @return
     */
    @ApiOperation(value = "根据投放申请guid查询所投放的代码集合", notes = "url传参")
    @GetMapping("/{guidDelivery}/deliveryLists")
    public ResultVO deliveryListDetail(@PathVariable
                                           @ApiParam(name = "guidDelivery", value = "投放申请guid", required = true)
                                           @NotNull(message = "投放申请id不能为空")
                                                   Integer guidDelivery) {
        return ResultVO.success("查询成功", sDeliveryService.selectDeliveryListByGuidDelivery(guidDelivery));
    }

    /**
     * 查询导出的投放申请
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "查询导出的投放申请", notes = "body传参")
    @PostMapping("/excels")
    public ResultVO selectOutDelivery(@RequestBody @Validated DeliveryOutExeclRequest request) {

        return ResultVO.success("查询成功", sDeliveryService.selectDeliveryOutExecl(request));
    }

    /**
     * 修改投放的时间及打包窗口
     *
     * @return
     */
    //@OperateLog(type = OperateType.UPDATE, desc = "修改投放的时间及打包窗口")
    @ApiOperation(value = "修改投放的时间及打包窗口", notes = "body传参")
    @PutMapping("/deliveryTimePackTime")
    public ResultVO updateDeliveryTimePackTime(@RequestBody @Validated SDeliveryUpdateRequest request) throws ParseException {

        sDeliveryService.updateDelivery(request);
        return ResultVO.success("修改成功");
    }

    /**
     * 查询运行环境的详情及时间窗口的验证
     *
     * @return
     */
    @ApiOperation(value = "查询运行环境的详情及时间窗口的验证", notes = "url传参")
    @GetMapping("/{guidDelivery}/profileDateilVerify")
    public ResultVO profileDateilVerify(@PathVariable
                                            @ApiParam(name = "guidDelivery", value = "投放申请guid", required = true)
                                            @NotNull(message = "投放申请id不能为空")
                                                    Integer guidDelivery) throws ParseException {

        return ResultVO.success("查询成功", sDeliveryService.selectProfileDeteilVerify(guidDelivery));
    }

    /**
     * 将投放申请投放到新环境中
     *
     * @return
     */
    //@OperateLog(type = OperateType.ADD, desc = "申请投放到新环境中")
    @ApiOperation(value = "将投放申请投放到新环境中", notes = "body传参")
    @PostMapping("/newProfiles")
    public ResultVO newProfiles(@RequestBody @Validated DeliveredNewProfilesRequest request) throws ParseException {
        return ResultVO.success("添加成功！", sDeliveryService.deliveredNewProfiles(request));
    }

    /**
     * 根据工作项的guid查询此工作项的投放状态
     * @return
     */
    @ApiOperation(value = "根据工作项的guid查询此工作项的投放状态", notes = "url传参")
    @GetMapping("/deliveredWay/{guidWorkitem}")
    public ResultVO deliveredWay(@PathVariable
                                     @ApiParam(name = "guidWorkitem", value = "工作项guid", required = true)
                                     @NotNull(message = "工作项id不能为空")
                                             Integer guidWorkitem){

        return ResultVO.success("校验成功",sDeliveryService.selectDeliveredWay(guidWorkitem));
    }

    /**
     * 查询要置为失败的投放申请
     * @return
     */
    @ApiOperation(value = "查询要置为失败的投放申请", notes = "url传参")
    @GetMapping("/applyingDelivered/{guidWorkitem}")
    public ResultVO applyingDelivered(@PathVariable
                                          @ApiParam(name = "guidWorkitem", value = "工作项guid", required = true)
                                          @NotNull(message = "工作项id不能为空")
                                                  Integer guidWorkitem){

        return ResultVO.success("查询成功",sDeliveryService.selectApplyingDelivery(guidWorkitem));
    }

    /**
     * 将投放申请作废
     * @return
     */
    //@OperateLog(type = OperateType.UPDATE, desc = "同批次申请作废")
    @ApiOperation(value = "将投放申请作废", notes = "body传参")
    @PostMapping("/buyFailed")
    public ResultVO buyFailed(@RequestBody
                                  @ApiParam(name = "guidDeliverys", value = "投放申请guid集合", required = true)
                                  @NotEmpty(message = "投放申请guid集合不能为空")List<Integer> guidDeliverys){
        sDeliveryService.delievryBuyFailed(guidDeliverys);

        return ResultVO.success("修改成功");
    }

    /**
     * 将一条申请置为失败
     * @param guidDelivery
     * @return
     */
    //@OperateLog(type = OperateType.UPDATE, desc = "一条申请置为失败")
    @ApiOperation(value = "将一条申请置为失败", notes = "url传参")
    @GetMapping("/{guidDelivery}/dislodgeDelivery")
    public ResultVO dislodgeDelivery(@PathVariable
                                     @ApiParam(name = "guidDelivery", value = "申请guid", required = true)
                                     @NotNull(message = "申请id不能为空")
                                             Integer guidDelivery){
        sDeliveryService.dislodgeDelivery(guidDelivery);
        return ResultVO.success("置失败成功");
    }

    /**
     * 投放申请预合并
     * @param guidDelivery
     * @return
     */
    @ApiOperation(value = "投放申请预合并", notes = "url传参")
    @GetMapping("/{guidDelivery}/forecast")
    public ResultVO forecastMerge (@PathVariable
                                   @ApiParam(name = "guidDelivery", value = "投放申请guid", required = true)
                                   @NotNull(message = "投放申请id不能为空")
                                           Integer guidDelivery) throws SVNException {

        MergeReportDetail mergeReportDetail = sDeliveryService.forecastMerge(guidDelivery, false);
        if (mergeReportDetail == null) {
            return ResultVO.success("合并无异常");
        }else {
            return ResultVO.success("合并异常", mergeReportDetail);
        }

    }

    /**
     * 投放申请真实合并
     * @param guidDelivery
     * @return
     */
    @ApiOperation(value = "投放申请真实合并", notes = "url传参")
    @GetMapping("/{guidDelivery}/real")
    public ResultVO realMerge (@PathVariable
                                   @ApiParam(name = "guidDelivery", value = "投放申请guid", required = true)
                                   @NotNull(message = "投放申请id不能为空")
                                           Integer guidDelivery) throws SVNException {

        MergeReportDetail mergeReportDetail = sDeliveryService.forecastMerge(guidDelivery, true);
        if (mergeReportDetail.getConflictReportDetails() == null){
            return ResultVO.success("代码文件已合并或代码文件并没有发生改变！");
        }else if (mergeReportDetail.isRealMerge()){
            return ResultVO.success("合并完成", mergeReportDetail);
        }else {
            return ResultVO.success("合并异常", mergeReportDetail);
        }
    }

    /**
     * 查询合并报告
     * @param guidDelivery
     * @return
     */
    @ApiOperation(value = "查询合并报告", notes = "url传参")
    @GetMapping("/{guidDelivery}/mergeReport")
    public ResultVO mergeReport(@PathVariable
                                    @ApiParam(name = "guidDelivery", value = "投放申请guid", required = true)
                                    @NotNull(message = "投放申请id不能为空")
                                            Integer guidDelivery){

        return ResultVO.success("查询成功", sDeliveryService.selectMergeReport(guidDelivery));
    }
}

